﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project.MVC.DataAccess
{
    public class CategoryAccess
    {
        Linq2SQLDataContext dataContext;
        public CategoryAccess()
        {
            dataContext = new Linq2SQLDataContext();
        }
        public IQueryable<Category> GetAllCategories(bool showHidden = false, int type = 1)
        {
            var query = from c in dataContext.Categories
                        orderby c.ParentCategoryId, c.DisplayOrder
                        where c.Type == type && c.Deleted == false
                        select c;

            var unsortedCategories = query.ToList();

            //sort categories
            var sortedCategories = SortCategoriesForTree(unsortedCategories, 0).Select(c => new Category { Id = c.Id, Name = GetCategoryBreadCrumb(c), DisplayOrder = c.DisplayOrder });

            return sortedCategories.AsQueryable();
        }

        public IList<Category> SortCategoriesForTree(IList<Category> source, int parentId)
        {
            var result = new List<Category>();

            var temp = source.ToList().FindAll(c => c.ParentCategoryId == parentId);
            foreach (var cat in temp)
            {
                //cat.Name = GetCategoryBreadCrumb(cat);
                result.Add(cat);
                result.AddRange(SortCategoriesForTree(source, cat.Id));
            }
            return result;
        }
        public string GetCategoryBreadCrumb(Category category)
        {
            string result = string.Empty;

            while (category != null && !category.Deleted)
            {
                if (String.IsNullOrEmpty(result))
                    result = category.Name;
                else
                    result = category.Name + " >> " + result;

                category = GetCategoryById(category.ParentCategoryId);

            }
            return result;
        }
        public Category GetCategoryById(int categoryId)
        {
            if (categoryId == 0)
                return null;


            var category = GetByKey(categoryId);
            return category;

        }

        public IQueryable<Category> GetList()
        {
            return dataContext.Categories;
        }
        #region Insert, update, delete
        public void Insert(Category _object)
        {
            dataContext.Categories.InsertOnSubmit(_object);
            dataContext.SubmitChanges();
        }
        public void Update(Category cate)
        {
            var cateDb = dataContext.Categories.Where(c => c.Id == cate.Id).SingleOrDefault();
            if (cateDb != null)
            {
                cateDb.Name = cate.Name;
                cateDb.DisplayOrder = cate.DisplayOrder;
                cateDb.UpdatedOnUtc = cate.UpdatedOnUtc;
                cateDb.DisplayOrder = cate.DisplayOrder;
                cateDb.ParentCategoryId = cate.ParentCategoryId;
            }
            dataContext.SubmitChanges();
        }
        public void Delete(Category cate)
        {
            var cateDb = dataContext.Categories.Where(c => c.Id == cate.Id).SingleOrDefault();
            if (cateDb != null)
            {
                cateDb.Deleted = true;
            }
            dataContext.SubmitChanges();
        }
        public Category GetByKey(int id)
        {
            return dataContext.Categories.Where(c => c.Id == id).SingleOrDefault();
        }
        #endregion
    }
}
